package clientLogin;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

public class JDBCClientDAO implements ClientDAOInterface {
	Connection con = null;

	public JDBCClientDAO() {
		try {
			Context namingContext = new InitialContext();
			DataSource dataSource = (DataSource) namingContext
					.lookup("java:comp/env/jdbc/schoolDatasource");
			con = dataSource.getConnection();
			System.out.println("Succes");
			con.setAutoCommit(false);

		} catch (Exception e) {
			System.out.println("Fail");
			e.printStackTrace();
		}
	}

	public Client getByLogin(String login) {
		Client client = new Client();

		try {
			Statement stmt = con.createStatement();
			ResultSet rs = stmt
					.executeQuery("SELECT * FROM T_CLIENT WHERE LOGIN = '"
							+ login + "'");

			if (rs.next()) {
				client.setLogin(login);
				client.setId(rs.getInt(1));
				client.setLastname(rs.getString(2));
				client.setFirstname(rs.getString(3));
				client.setAddress(rs.getString(4));
				client.setPhone(rs.getString(5));
				client.setEmail(rs.getString(6));
				client.setPwd(rs.getString(8));
			}
			con.commit();
		} catch (Exception e) {
			System.out.println("There is No such Login");
			e.printStackTrace();
		}
		return client;
	}

	public void create(Client client) {
		try {
			String fn = client.getFirstname();
			String ln = client.getLastname();
			String add = client.getAddress();
			String phone = client.getPhone();
			String email = client.getEmail();
			String login = client.getLogin();
			String pwd = client.getPwd();
			Statement stmt = con.createStatement();
			stmt.executeUpdate("insert into T_CLIENT(LASTNAME,FIRSTNAME,ADDRESS,PHONE,EMAIL,LOGIN,PWD) values ('"
					+ ln
					+ "' , '"
					+ fn
					+ "' , '"
					+ add
					+ "' , '"
					+ phone
					+ "' , '" + email + "' , '" + login + "' , '" + pwd + "')");
			con.commit();
		} catch (Exception e) {
			System.out.println("Fail");
			e.printStackTrace();
		}
	}

	public void update(Client client) {
		try {
			int id = client.getId();
			String sql = "UPDATE T_CLIENT SET FIRSTNAME ='"
					+ client.getFirstname() + "', LASTNAME = '"
					+ client.getLastname() + "', ADDRESS = '"
					+ client.getAddress() + "', PHONE = '" + client.getPhone()
					+ "', EMAIL = '" + client.getEmail() + "' WHERE ID = "
					+ id;
			Statement stmt = con.createStatement();
			stmt.executeUpdate(sql);

			con.commit();
		} catch (Exception e) {
			System.out.println("Fail");
			e.printStackTrace();
		}
	}

	@Override
	public boolean searchByLogin(String login) {
		boolean exist = true;
		try {
			Statement stmt = con.createStatement();
			ResultSet rs = stmt
					.executeQuery("SELECT LOGIN FROM T_CLIENT WHERE LOGIN = '"
							+ login + "'");
			if (!rs.next()) {
				exist = false;
			}
			con.commit();
		} catch (Exception e) {
			System.out.println("Fail");
			e.printStackTrace();
		}
		return exist;
	}

	public void updatePwd(Client client, String pwd) {
		try {
			int id = client.getId();
			Statement stmt = con.createStatement();
			ResultSet rs = stmt
					.executeQuery("SELECT PWD FROM T_CLIENT WHERE ID = '" + id
							+ "'");
			if (rs.next()) {
				
				stmt.executeUpdate("UPDATE T_CLIENT SET PWD = '" + pwd
						+ "'  WHERE ID = '" + id + "'");
			}
			con.commit();
		} catch (Exception e) {
			System.out.println("Fail");
			e.printStackTrace();
		}
	}

	public Client getByID(int id) {
		Client client = new Client();
		try {
			Statement stmt = con.createStatement();
			ResultSet rs = stmt
					.executeQuery("SELECT * FROM T_CLIENT WHERE ID = '" + id
							+ "'");
			if (rs.next()) {
			
				client.setId(rs.getInt(1));
				client.setLastname(rs.getString(2));
				client.setFirstname(rs.getString(3));
				client.setAddress(rs.getString(4));
				client.setPhone(rs.getString(5));
				client.setEmail(rs.getString(6));
				client.setLogin(rs.getString(7));
				client.setPwd(rs.getString(8));
			}
			con.commit();
		} catch (Exception e) {
			System.out.println("No such ID");
			e.printStackTrace();
		}
		return client;
	}

}
